package cate.game.role.res.magiccontract;

import cate.common.table.activity.magiccontract.row.MagicContractBaseRow;
import cate.common.table.activity.magiccontract.row.MagicContractUnlockRow;
import cate.common.table.item.hero.row.HeroBaseRow;
import cate.common.util.GameResult;
import cate.game.attr.FightAttr;
import cate.game.client.msg.MergeMsgSender;
import cate.game.res.RewardShow;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.bag.hero.Hero;
import cate.game.role.res.magiccontract.msg.HeroOnDeskResp;
import cate.game.train.hero.TrainHeroHandler;
import cate.game.train.hero.msg.HeroLockResp;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class MagicContractDesk extends RoleDependent {
    @NoteField(value = "key:位置，value:英雄uid")
    public Map<Integer, String> contractMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (contractMap == null) {
            contractMap = new HashMap<>();
        }
    }

    public MagicContractDesk() {
    }

    public MagicContractDesk(Role role) {
        super(role);
        initialize(role);
    }

    public GameResult<?> putHero(int pos, Hero hero, MagicContractBaseRow baseRow) {
        GameResult<?> r = new GameResult<>();
        MagicContractUnlockRow row = findRow(pos);
        if (row == null) {
            return r.fail("格子不存在");
        }
        if (role.getRes().worldTree.heroShare.shareLvl < row.unlockLevel || row.unlockLevel <= 0) {
            return r.fail("格子未开启");
        }
        int star = hero.getTpl(role.toPlayBuildContext()).star;
        if (star < baseRow.minStar || star > baseRow.maxStar) {
            return r.fail("英雄星级错误");
        }
        if (hero.lock.heroYoke) {
            return r.fail("英雄已羁绊");
        }
        for (Integer p :
                contractMap.keySet()) {
            String uid = contractMap.get(p);
            if (uid == null) {
                continue;
            }
            if (p == pos) {
                continue;
            }
            Hero h = findHero(uid);
            if (h == null) {
                return r.fail("灵契台英雄错误");
            }
            if (h.getTpl(role.toPlayBuildContext()) == null) {
                return r.fail("英雄配置出错");
            }
            if (h.getTpl(role.toPlayBuildContext()).protoId == hero.getTpl(role.toPlayBuildContext()).protoId) {
                return r.fail("灵契台不能上两个相同的英雄");
            }
        }
        MergeMsgSender sender = new MergeMsgSender();
        RewardShow show = hero.offAll(role, sender);
        if (contractMap.get(pos) != null) {
            offHero(pos);
        }
        contractMap.put(pos, hero.uid);
        hero.lock.heroContract = true;
        sender.send(role,true);
        role.sendNow(new HeroOnDeskResp(hero.uid, hero,show));
        role.sendNow(new HeroLockResp(hero.lock));
        return r;
    }

    public GameResult<?> offHero(int pos) {
        GameResult<?> r = new GameResult<>();
        MagicContractUnlockRow row = findRow(pos);
        if (row == null) {
            return r.fail("格子不存在");
        }
        if (role.getRes().worldTree.heroShare.shareLvl < row.unlockLevel || row.unlockLevel <= 0) {
            return r.fail("格子未开启");
        }
        if (!contractMap.containsKey(pos)) {
            return r.fail("格子为空");
        }
        Hero hero = findHero(contractMap.get(pos));
        if (hero == null) {
            return r.fail("英雄不存在");
        }
        hero.lock.heroContract = false;
        contractMap.remove(pos);
        role.sendNow(new HeroLockResp(hero.lock));
        return r;
    }


    public void clear() {
        contractMap.values().forEach(uid -> {
            Hero hero = findHero(uid);
            if (hero == null) {
                return;
            }
            hero.lock.heroContract = false;
            role.sendNow(new HeroLockResp(hero.lock));
        });
        contractMap.clear();
    }

    public MagicContractUnlockRow findRow(int id) {
        return role.getGame().table.magicContract.unlock.get(id);
    }

    private Hero findHero(String heroUid) {
        if (StringUtils.isBlank(heroUid)) {
            return null;
        }
        return role.getBag().hero.getItem(heroUid);
    }

    public FightAttr addAttr() {
        FightAttr f = new FightAttr();
        contractMap.values().forEach(uid -> {
            Hero hero = findHero(uid);
            if (hero == null) {
                return;
            }
            HeroBaseRow tpl = hero.getTpl(role.toPlayBuildContext());
            if (tpl == null) {
                return;
            }
            f.add(new FightAttr(tpl.magicAttr));
        });
        return f;
    }
}
